﻿using System;
using TrafficArchives.Common;
using TrafficArchives.DataAccess;

namespace TrafficArchives.SafeEducate
{
    public partial class SafeEducateView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initilize();

                QueryData();
            }
        }

        private void Initilize()
        {
            TxtStartDate.Attributes.Add("onclick", "WdatePicker()");
            TxtEndDate.Attributes.Add("onclick", "WdatePicker()");
        }

        private void QueryData()
        {
            var request = new Request { Pagination = { Enabled = true, PageSize = MainPager.PageSize, PageIndex = MainPager.CurrentPageIndex - 1 } };
            
            if (!string.IsNullOrEmpty(TxtStartDate.Text))
            {
                var con = new Condition
                {
                    FieldName = "to_char(t.PXDATE, 'yyyy-mm-dd')",
                    Operator = ConditionOperator.GreaterAndEqual,
                    Values = new object[] { TxtStartDate.Text }
                };
                request.Items.Add(con);
            }

            if (!string.IsNullOrEmpty(TxtEndDate.Text))
            {
                var con = new Condition
                {
                    FieldName = "to_char(t.PXDATE, 'yyyy-mm-dd')",
                    Operator = ConditionOperator.LessAndEqual,
                    Values = new object[] { TxtEndDate.Text }
                };
                request.Items.Add(con);
            }

            var db = new SafeEducateDb();
            var data = db.GetData(request);
            MainPager.RecordCount = data.RecordCount;
            GridViewMain.DataSource = data.Data;
            GridViewMain.DataBind();

            // 没数据时，显示表头。
            TableHeader.Visible = data.RecordCount == 0;
            UpdatePanelMain.Update();
        }

        protected void MainPagerPageIndexChanged(object sender, EventArgs e)
        {
            QueryData();
        }

        protected void BtnQueryClicked(object sender, EventArgs e)
        {
            MainPager.CurrentPageIndex = 1;

            QueryData();
        }
    }
}